//
//  RealNameAuthenticationVC.m
//  MRBang
//
//  Created by 铅笔 on 15/12/16.
//  Copyright © 2015年 杭州易网新科技. All rights reserved.
//

#import "RealNameAuthenticationVC.h"
#import "JHAPISDK.h"
#import "JHOpenidSupplier.h"

@interface RealNameAuthenticationVC ()

@property (nonatomic,strong) UITextField *textField_realName;
@property (nonatomic,strong) UITextField *textField_IDCardNo;

@property (nonatomic,strong) UIButton *button_submit;

@end

@implementation RealNameAuthenticationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"实名认证";
    
    [[JHOpenidSupplier shareSupplier] registerJuheAPIByOpenId:@"JH2a87f8cc83880c2a32fcbe59f4b1a896"];
    
    //创建子视图
    [self initWithAllSubviews];
    
}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = YES;
}

#pragma mark ---- 创建子视图
- (void) initWithAllSubviews
{
    
    self.textField_realName = [[UITextField alloc] initWithFrame:CGRectMake(45*PROPORTION_WIDTH,64+70*PROPORTION_WIDTH,kScreenWidth - 45*2*PROPORTION_WIDTH, 70*PROPORTION_WIDTH)];
    [self.view addSubview:self.textField_realName];
    
    if (self.str_name.length!=0) {
        self.textField_realName.text = self.str_name;
    }
    else
    {
        self.textField_realName.placeholder = @"  请输入真实姓名";
    }
    self.textField_realName.font = [UIFont systemFontOfSize:Font_SIZE_18];
    self.textField_realName.layer.borderColor = RXDColor(228, 228, 228).CGColor;
    self.textField_realName.layer.borderWidth = 1.0;
    self.textField_realName.layer.cornerRadius = 5.0;
    self.textField_realName.clipsToBounds = YES;
    
    self.textField_IDCardNo = [[UITextField alloc] initWithFrame:CGRectMake(self.textField_realName.x,self.textField_realName.bottom+35*PROPORTION_WIDTH,self.textField_realName.width, self.textField_realName.height)];
    [self.view addSubview:self.textField_IDCardNo];
    
    if (self.str_idcard.length!=0) {
        self.textField_IDCardNo.text = self.str_idcard;
    }
    else
    {
        self.textField_IDCardNo.placeholder = @"  身份证号码";
    }
    
    self.textField_IDCardNo.font = [UIFont systemFontOfSize:Font_SIZE_18];
    self.textField_IDCardNo.layer.borderColor = RXDColor(228, 228, 228).CGColor;
    self.textField_IDCardNo.layer.borderWidth = 1.0;
    self.textField_IDCardNo.layer.cornerRadius = 5.0;
    self.textField_IDCardNo.clipsToBounds = YES;
    
    //label 提示字样
    UILabel *label_text = [[UILabel alloc] initWithFrame:CGRectMake(20, self.textField_IDCardNo.frame.size.height+self.textField_IDCardNo.frame.origin.y+20, kScreenWidth-40, 30)];
    [self.view addSubview:label_text];
    label_text.text = @"申请信用卡、贷款需要实名认证,请输入真实姓名和身份证号码";
    label_text.font = [UIFont systemFontOfSize:Font_SIZE_18];
    label_text.numberOfLines = 2;
    [label_text sizeToFit];
    label_text.textColor = RXDColor(121, 121, 121);

    //设置修改button
    self.button_submit = [[UIButton alloc] initWithFrame:CGRectMake(20, self.textField_IDCardNo.bottom+85*PROPORTION_WIDTH,self.textField_IDCardNo.width, 95*PROPORTION_WIDTH)];
    [self.view addSubview:self.button_submit];
    [self.button_submit.titleLabel setFont:[UIFont systemFontOfSize:Font_SIZE_22*2]];
    self.button_submit.backgroundColor = RXDColor(255, 118, 112);
    [self.button_submit setTitle:@"提交" forState:UIControlStateNormal];
    [self.button_submit addTarget:self action:@selector(submitRealNameButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.button_submit.layer.cornerRadius = 3.0;
    self.button_submit.clipsToBounds = YES;
}

#pragma mark --- 实名认证 提交
- (void) submitRealNameButton
{
    if (self.textField_IDCardNo.text.length!=0 && self.textField_realName.text.length!=0)
    {
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        [self checkIsEnxtens];
    }
    else
    {
       [BaseViewController showMessage:@"请完善信息"];
    }
}

#pragma mark ---- 网络数据请求
//验证是否匹配
- (void) checkIsEnxtens
{
    
    [self dismissKeyBoard];
    
    //    ***************** LIFE ***************
    //    /*IP*/
    NSString *path = @"http://op.juhe.cn/idcard/query";
    NSString *api_id = @"103";
    NSString *method = @"GET";
    
    NSDictionary *param = @{@"idcard":self.textField_IDCardNo.text, @"realname":self.textField_realName.text,@"key":@"942272c5952cd1dfe6562ac44413947b"};
    JHAPISDK *juheapi = [JHAPISDK shareJHAPISDK];
    
    [juheapi executeWorkWithAPI:path
                          APIID:api_id
                     Parameters:param
                         Method:method
                        Success:^(id responseObject){
                            if ([[param objectForKey:@"dtype"] isEqualToString:@"xml"]) {
                                
                                NSLog(@"***xml*** \n %@", responseObject);
                                
                            }else{
                                int error_code = [[responseObject objectForKey:@"error_code"] intValue];
                                
                                if (!error_code) {
                                    
                                    NSLog(@" %@", responseObject);
                                    NSDictionary *dic_result = responseObject[@"result"];
                                    if (dic_result.count!=0) {
                                        
                                         int str_res = [dic_result[@"res"] intValue];
                                        
                                        if (str_res == 1) {
                                            [self dataSourceRequestRealName];
                                        }
                                        if (str_res == 2)
                                        {
                                            [BaseViewController showMessage:@"姓名或身份证号不匹配"];
                                        }
                                        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
                                    }
                                    
                                }else{
                                    NSLog(@" %@", responseObject);
                                    [BaseViewController showMessage:responseObject[@"reason"]];
                                    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
                                    
                                }
                            }
                        } Failure:^(NSError *error) {
                            NSLog(@"error:   333333%@",error.description);
                            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];

                        }];
}

//提交到我们的信息 网络请求
- (void) dataSourceRequestRealName
{
    Reachability *reach = [Reachability reachabilityForInternetConnection];
    NetworkStatus status = [reach currentReachabilityStatus];
    
    if (![UIView NetFromStatus:status]) {
        NSLog(@"没有网络");
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        [UIView showAlertMsg:@"网络连接已断开" andShowBgColor:[UIColor blackColor] andLabelTectColor:[UIColor whiteColor]];
        
        return;
    }
    else
    {
        NSDictionary *paras_realName = @{@"memberid":[[NSUserDefaults standardUserDefaults] objectForKey:@"memberid"],@"name":self.textField_realName.text,@"idcard":self.textField_IDCardNo.text};
        
        [[HttpRequest shardWebUtil] postNetworkRequestURLString:[NSString stringWithFormat:@"%@member/authentication",MRAPI] parameters:paras_realName success:^(id obj) {
            
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            
            NSString *str_false = obj[@"error"];
            
            if (str_false.length==0)
            {
                [BaseViewController showMessage:@"提交成功"];
                [self.navigationController popViewControllerAnimated:NO];
            }
            else
            {
                [BaseViewController showMessage:str_false];
            }
            
        } fail:^(NSError *error) {
            
        }];
    }
}

- (void) touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

-(void)dismissKeyBoard
{
    [self.textField_realName resignFirstResponder];
    [self.textField_IDCardNo resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
